package com.caigou.task;

import com.caigou.bean.cms.entity.AdConfig;
import com.caigou.bean.cms.entity.AdList;
import com.caigou.component.HtmlBuildAdComponent;
import com.caigou.component.RedisComponent;
import com.caigou.service.AdConfigService;
import com.caigou.service.AdListService;
import com.caigou.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Slf4j
@Component
@EnableScheduling
public class AdTask {

    @Resource
    private AdConfigService adConfigService;
    @Resource
    private AdListService adListService;
    @Resource
    private HtmlBuildAdComponent htmlBuildAdComponent;
    @Resource
    private RedisComponent redisComponent;

//    @Scheduled(cron = "0 */10 * * * ?")// 分钟
    @Scheduled(cron =  "0 15 0 * * ?")// 每日早上0点15分执行
    public void changeAdTask(){
        // 判别任务同步锁
        String key = "task:lock:adTaskKey";// 旧数据同步锁的key
        boolean lockFlag = redisComponent.setLock(key, 3000);
        if (!lockFlag){ log.info("广告定时器任务锁获取失败");return; }
        // 获取广告列表中达到开始时间或者达到结束时间的
        List<AdList> list = adListService.loadAdListByTask();
        long nowTime = new Date().getTime();
        log.info("开始执行广告定时器任务");
        if (null != list){
            list.forEach(item -> {
                // 获取广告位配置信息
                AdConfig adConfig = adConfigService.loadAdConfigByAdId(item.getAdConfigId());
                // 格式化开始时间和结束时间
                Date start = DateUtil.stringtoDate(item.getStartTime(),"yyyy-MM-dd");
                Date end = DateUtil.stringtoDate(item.getEndTime(),"yyyy-MM-dd");
                // 判定要执行哪个操作
                if (start.getTime() <= nowTime && nowTime < end.getTime() && item.getAdStatus() == 0){
                    // 设置当前广告位的预设广告为下架状态
                    adListService.updateAdListToDefault(item.getAdConfigId(),0);
                    // 设置当前广告为上架状态
                    adListService.updateAdListStatus(item.getId(), 1);
                    log.info("下架预设广告-->转为当前广告");
                    // 执行完循环之后更新当前广告位碎片
                    htmlBuildAdComponent.buildAdHtml(adConfig.getAdCode());
                } else if (start.getTime() < nowTime && nowTime > end.getTime() && item.getAdStatus() == 1){
                    // 设置当前广告的应用状态为过期状态
                    adListService.updateAdListStatus(item.getId(), -1);
                    // 将当前广告位预设广告设为应用状态
                    if (!adConfig.getAdCode().equals("A01")){// 除去A01广告位之外都将默认广告位开启
                        adListService.updateAdListToDefault(item.getAdConfigId(),1);
                    }
                    log.info("下架当前广告-->转为预设广告");
                    // 执行完循环之后更新当前广告位碎片
                    htmlBuildAdComponent.buildAdHtml(adConfig.getAdCode());
                } else {
                    log.info("不需要处理的数据");
                }
            });
        }
        boolean releaseFlag = redisComponent.releaseLock(key);
        log.info("广告定时器任务执行完成 释放锁"+releaseFlag);
    }
}
